<?php
/**
  * @author Mazvv
 * @package Cities
 */
class Officialcources extends CRM_Controller {
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'officialcources';
		$this->lang->load('officialcources');
		$this->load->model('officialcources_model');
		$this->SetNewToolSize('600x300');
		$this->SetEditToolSize('600x300');
		$this->SetDetailsToolSize('600x300');
		$this->SetFindToolSize('600x300');
		
		$this->a_validation_rules['date_doc'] = "required";
		
		$this->a_validation_rules['_currencies_rid'] = "required";     
		$this->a_validation_rules['_base_currencies_rid'] = "required";             
		$this->a_validation_rules['cource_date'] = "required";              
		$this->a_validation_rules['cource_value'] = "required";
			
		$this->a_validation_fields['date_doc'] = $this->lang->line('DATE_DOC');
		$this->a_validation_fields['_currencies_rid'] = $this->lang->line('CURRENCY'); 
		$this->a_validation_fields['cource_date'] = $this->lang->line('COURCE_DATE'); 
		$this->a_validation_fields['cource_value'] = $this->lang->line('COURCE_VALUE');

		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_cif_name){
		switch ($m_cif_name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}			
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 

		$content['date_doc'] = $this->lang->line('DATE_DOC');
		$content['currency_code'] = $this->lang->line('CURRENCY');
		$content['cource_date'] = $this->lang->line('COURCE_DATE'); 
		$content['cource_value'] = $this->lang->line('COURCE_VALUE'); 
		
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->officialcources_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if($content['DS'] && !$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('officialcources/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE'); 
			
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data = array('name'=>'date_doc', 'id'=>'date_doc', 'size'=>'12', 'value'=>$this->validation->date_doc?$this->validation->date_doc:date('d.m.Y')); 
			$content['date_doc_field'] =  form_input($data);
			# } date_doc
			
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->officialcources_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies, $this->validation->_currencies_rid?$this->validation->_currencies_rid:'');
			# } _currencies_rid

			# { cource_date
			$content['cource_date'] = $this->RenderLabel('cource_date');
			$data = array('name'=>'cource_date', 'id'=>'cource_date', 'value'=>$this->validation->cource_date?$this->validation->cource_date:'', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['cource_date_field'] =  form_input($data).$this->RenderCalendar('cource_date');
			# } cource_date

			# { cource_value
			$content['cource_value'] = $this->RenderLabel('cource_value');
			$data = array('name'=>'cource_value', 'id'=>'cource_value', 'value'=>$this->validation->cource_value?$this->validation->cource_value:'', 'size'=>'12'); 
			$content['cource_value_field'] =  form_input($data);
			# } cource_value
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			
			return $this->load->view('officialcources/create', $content, True);
		}
		if($insRid = $this->officialcources_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->officialcources_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 
			
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data = array('name'=>'date_doc', 'id'=>'date_doc', 'size'=>'12', 'value'=>$this->validation->date_doc?$this->validation->date_doc:$content['DS']->date_doc); 
			$content['date_doc_field'] =  form_input($data);
			# } date_doc
			
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->officialcources_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies, $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			# } _currencies_rid

			# { cource_date
			$content['cource_date'] = $this->RenderLabel('cource_date');
			$data = array('name'=>'cource_date', 'id'=>'cource_date', 'value'=>$this->validation->cource_date?$this->validation->cource_date:$content['DS']->cource_date, 'readonly'=>'readonly', 'size'=>'12'); 
			$content['cource_date_field'] =  form_input($data).$this->RenderCalendar('cource_date');
			# } cource_date

			# { cource_value
			$content['cource_value'] = $this->RenderLabel('cource_value');
			$data = array('name'=>'cource_value', 'id'=>'cource_value', 'value'=>$this->validation->cource_value?$this->validation->cource_value:$content['DS']->cource_value, 'size'=>'12'); 
			$content['cource_value_field'] =  form_input($data);
			# } cource_value
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('officialcources/edit', $content, True);
		}
		if($this->officialcources_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->officialcources_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data = array('name'=>'date_doc', 'id'=>'date_doc', 'size'=>'12', 'value'=>$this->validation->date_doc?$this->validation->date_doc:$content['DS']->date_doc); 
			$content['date_doc_field'] =  form_input($data);
			# } date_doc
			
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->officialcources_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies, $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			# } _currencies_rid

			# { cource_date
			$content['cource_date'] = $this->RenderLabel('cource_date');
			$data = array('name'=>'cource_date', 'id'=>'cource_date', 'value'=>$this->validation->cource_date?$this->validation->cource_date:$content['DS']->cource_date, 'size'=>'12'); 
			$content['cource_date_field'] =  form_input($data);
			# } cource_date

			# { cource_value
			$content['cource_value'] = $this->RenderLabel('cource_value');
			$data = array('name'=>'cource_value', 'id'=>'cource_value', 'value'=>$this->validation->cource_value?$this->validation->cource_value:$content['DS']->cource_value, 'size'=>'12'); 
			$content['cource_value_field'] =  form_input($data);
			# } cource_value
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			
			return $this->load->view('officialcources/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->officialcources_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_FIND')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_FIND'); 
			
			# { date_doc_from
			$content['date_doc_from'] = $this->lang->line('DATE_DOC_FROM');
			$data = array('name'=>'date_doc_from', 'id'=>'date_doc_from', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['date_doc_from_field'] = form_input($data).$this->RenderCalendar('date_doc_from');
			# } date_doc_from

			# { date_doc_to
			$content['date_doc_to'] = $this->lang->line('DATE_DOC_TO');
			$data = array('name'=>'date_doc_to', 'id'=>'date_doc_to', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['date_doc_to_field'] = form_input($data).$this->RenderCalendar('date_doc_to');
			# } date_doc_to
									
			# { _currencies_rid
			$currencies = array(' '=>'');
			foreach($this->officialcources_model->GetCurrenciesList() as $currency){
				$currencies[$currency->rid] = $currency->currency_code;
			}
			$content['_currencies_rid'] = $this->lang->line('CURRENCY');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $currencies);
			# } _currencies_rid

			# { cource_date_from
			$content['cource_date_from'] = $this->lang->line('COURCE_DATE_FROM');
			$data = array('name'=>'cource_date_from', 'id'=>'cource_date_from', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['cource_date_from_field'] =  form_input($data).$this->RenderCalendar('cource_date_from');
			# } cource_date_from

			# { cource_date_to
			$content['cource_date_to'] = $this->lang->line('COURCE_DATE_TO');
			$data = array('name'=>'cource_date_to', 'id'=>'cource_date_to', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['cource_date_to_field'] =  form_input($data).$this->RenderCalendar('cource_date_to');
			# } cource_date_to
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array(' '=>'','0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
						
			return $this->load->view('officialcources/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['date_doc_from']) && trim($_POST['date_doc_from'])) $searchRule['_officialcources_headers.date_doc>='] = trim($_POST['date_doc_from']);
		if(isset($_POST['date_doc_to']) && trim($_POST['date_doc_to'])) $searchRule['_officialcources_headers.date_doc<='] = trim($_POST['date_doc_to']);
		if(isset($_POST['cource_date_from']) && trim($_POST['cource_date_from'])) $searchRule['_officialcources_rows.cource_date>='] = trim($_POST['cource_date_from']);
		if(isset($_POST['cource_date_to']) && trim($_POST['cource_date_to'])) $searchRule['_officialcources_rows.cource_date<='] = trim($_POST['cource_date_to']);
		if(isset($_POST['_currencies_rid']) && trim($_POST['_currencies_rid'])) $searchRule['_officialcources_rows._currencies_rid'] = trim($_POST['_currencies_rid']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_documents.descr'] = trim($_POST['descr']);
		if(isset($_POST['archive']) && trim($_POST['archive'])) $searchRule['_documents.archive'] = trim($_POST['archive']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('officialcources', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
}

?>